package cn.dyp.com.automaticcontrolserver.action;


import org.json.JSONException;
import org.json.JSONObject;

import cn.dyp.com.automaticcontrolserver.bean.Sensor;
import cn.dyp.com.automaticcontrolserver.serial.SerialportService;
import cn.dyp.com.automaticcontrolserver.util.Util;

/**
 * @author zongbingwu 获取所有传感器值的请求处理类
 */
public class GetSensorAction extends BaseAction {
	// action name
	public static final String TAG = "getSensor";

	@Override
	protected String jasonPorcess(String param) {
		JSONObject jsonResponse = new JSONObject();
		try {
			jsonResponse.put("PM25",
					SerialportService.getSensorValueByName(Sensor.PM25_SENSOR));// PM2.5的值
			jsonResponse.put("co2",
					SerialportService.getSensorValueByName(Sensor.CO2_SENSOR));// co2浓度

			jsonResponse.put("airTemperature", SerialportService
					.getSensorValueByName(Sensor.AIR_TMPER_SENSOR));// 空气温度
			jsonResponse.put("airHumidity", SerialportService
					.getSensorValueByName(Sensor.AIR_HUMID_SENSOR));// 空气湿度
			jsonResponse.put("soilTemperature", SerialportService
					.getSensorValueByName(Sensor.SOIL_TMPER_SENSOR));// 土壤温度
			jsonResponse.put("soilHumidity", SerialportService
					.getSensorValueByName(Sensor.SOIL_HUMID_SENSOR));// 土壤湿度
			jsonResponse
					.put("light", SerialportService
							.getSensorValueByName(Sensor.LIGHT_SENSOR));// 光照强度

			jsonResponse.put("result", "ok");
			logMsg = "Get sensor value";
			logObj = "All Sensor";
			return jsonResponse.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected String soapPorcess(String param) {
		String soapBody = "";
		soapBody += Util
				.getXmlElementStr(
						"PM25",
						SerialportService
								.getSensorValueByName(Sensor.PM25_SENSOR) + "");// PM2.5的值
		soapBody += Util.getXmlElementStr("co2",
				SerialportService.getSensorValueByName(Sensor.CO2_SENSOR) + "");// co2浓度

		soapBody += Util.getXmlElementStr("airTemperature",
				SerialportService.getSensorValueByName(Sensor.AIR_TMPER_SENSOR)
						+ "");// 空气温度
		soapBody += Util.getXmlElementStr("airHumidity",
				SerialportService.getSensorValueByName(Sensor.AIR_HUMID_SENSOR)
						+ "");// 空气湿度
		soapBody += Util.getXmlElementStr(
				"soilTemperature",
				SerialportService
						.getSensorValueByName(Sensor.SOIL_TMPER_SENSOR) + "");// 土壤温度
		soapBody += Util.getXmlElementStr(
				"soilHumidity",
				SerialportService
						.getSensorValueByName(Sensor.SOIL_HUMID_SENSOR) + "");// 土壤湿度
		soapBody += Util.getXmlElementStr("light",
				SerialportService.getSensorValueByName(Sensor.LIGHT_SENSOR)
						+ "");// 光照强度

		soapBody += Util.getXmlElementStr("result", "ok");
		logMsg = "Get sensor value";
		logObj = "All Sensor";
		return Util.getSoapXml(soapBody);
	}

}
